<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="${package}.${moduleName}.dao.${className}Dao">

    <resultMap type="${className}Dto" id="BaseResultMap">
    <#list columns as column>
        <result property="${column.attrname}" column="${column.columnName}"/>
    </#list>
    </resultMap>

    <sql id="Base_Column_List" >
    <#list columns as column>${column.columnName}<#if (column_has_next)>,</#if> </#list>
    </sql>

    <select id="queryByPrimaryKey" resultType="${className}Dto">
        select  <include refid="Base_Column_List" /> from ${tableName}  where ${pk.columnName} = ${"#{" + pk.attrname + "}"}
    </select>

    <select id="queryByCondition" resultType="${className}Dto">
        select <include refid="Base_Column_List" /> from ${tableName}
        <where>
        <#list columns as column>
         <if test="${column.attrname} != null">
            and ${column.columnName} = ${"#{" + column.attrname + "}"}
         </if>
        </#list>
        </where>

    </select>

    <insert id="insert" parameterType="${className}Dto">
        insert into ${tableName}
        (
        <#list columns as column>
            ${column.columnName} <#if (column_has_next)>, </#if>
        </#list>
        )
        values
        (
        <#list columns as column>
            ${"#{"+column.attrname+"}"} <#if (column_has_next)>, </#if>
        </#list>
        )
    </insert>


    <insert id="insertSelective" parameterType="${className}Dto">
        insert into ${tableName}
        <trim prefix="(" suffix=")" suffixOverrides="," >
        <#list columns as column>
            <if test="${column.attrname} != null">
            ${column.columnName},
            </if>
        </#list>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides="," >
        <#list columns as column>
            <if test="${column.attrname} != null">
            ${"#{"+column.attrname+"}"},
            </if>
        </#list>
        </trim>
    </insert>


    <update id="updateByPrimaryKey" parameterType="${className}Dto">
        update ${tableName} set
        <#list columns as column>
            ${column.columnName} = ${"#{"+column.attrname+"}"} <#if (column_has_next)>, </#if>
        </#list>
        where ${pk.columnName} = ${"#{"+pk.attrname+"}"}
    </update>

    <update id="updateSelective" parameterType="${className}Dto">
        update ${tableName}
        <set >
        <#list columns as column>
            <if test="${column.attrname} != null" >
            ${column.columnName} = ${"#{"+column.attrname+"}"},
            </if>
        </#list>
        </set>
        where ${pk.columnName} = ${"#{"+pk.attrname+"}"}
    </update>

    <delete id="deleteByPrimaryKey">
        delete from ${tableName} where ${pk.columnName} = ${"#{"+pk.attrname+"}"}
    </delete>

    <delete id="deleteBatchByPrimaryKey">
        delete from ${tableName} where ${pk.columnName} in
        <foreach item="item" collection="list" open="(" separator="," close=")">
        ${r"#{item}"}
        </foreach>
    </delete>

</mapper>
